<?php

class UserController extends My_Controller_Base 
{
	public function init() {
		$this->view->baseUrl = $this->_request->getBaseUrl();
		$oSesja = new Zend_Session_Namespace('zalogowanyUzytkownik');
		$this->view->user = $oSesja->oUzytkownik;
	}
	
    public function loginAction() 
    {
  		if (!$this->getRequest()->isPost()) {
            $oForm = new LoginForm(); 
    		$this->view->form = $oForm;
        } else {
        	$oForm = new LoginForm();
        	if (!$oForm->isValid($_POST)) {
            	$this->view->form = $oForm;
        	} else {
        		$aValues = $oForm->getValues();
        		
				$oSesja = new Zend_Session_Namespace('zalogowanyUzytkownik');
    			$bOk = $oSesja->oUzytkownik->login($aValues['email'], $aValues['password']);

        		if($bOk) {
        			$this->_redirect('index/index');
        		} else {
        			$this->view->form = $oForm;
        			$this->view->error = 'Błędny adres email lub hasło.';
        		}
        	}
        }
    }
    
    public function logoutAction() 
    {
   		$oSesja = new Zend_Session_Namespace('zalogowanyUzytkownik');
		$oSesja->oUzytkownik->logout();
		$this->_redirect('index/index');	   
    }

}
?>